package com.ala4.oxcafe.common.utils;

import com.ala4.oxcafe.exception.JSONException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * json工具类
 *
 * @author PING
 * @version 1.0.0
 * @date 2025/7/31 14:49
 */
@Slf4j
@Component
public class JSONUtil implements ApplicationContextAware {

    private static ObjectMapper objectMapper;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        objectMapper = applicationContext.getBean(ObjectMapper.class);
    }

    /**
     * 对象转JSON字符串
     *
     * @param obj Java对象
     * @return JSON字符串
     */
    public static String toJsonStr(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new JSONException("json to string error", e);
        }
    }

    /**
     * JSON字符串转对象（支持简单类型）
     *
     * @param json  JSON字符串
     * @param clazz 目标类型
     * @param <T>   泛型类型
     * @return Java对象
     */
    public static <T> T toBean(String json, Class<T> clazz) {
        try {
            return objectMapper.readValue(json, clazz);
        } catch (JsonProcessingException e) {
            throw new JSONException("json to bean error", e);
        }
    }

    /**
     * JSON字符串转复杂泛型对象
     *
     * @param json          JSON字符串
     * @param typeReference 类型引用（如 new TypeReference<List<User>>(){}）
     * @param <T>           泛型类型
     * @return Java对象
     */
    public static <T> T toBean(String json, TypeReference<T> typeReference) {
        try {
            return objectMapper.readValue(json, typeReference);
        } catch (JsonProcessingException e) {
            throw new JSONException("json to bean error", e);
        }
    }

    /**
     * JSON字符串转集合
     *
     * @param json      JSON字符串
     * @param itemClass 集合元素类型
     * @param <T>       泛型类型
     * @return 集合对象
     */
    public static <T> List<T> toList(String json, Class<T> itemClass) {
        try {
            JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, itemClass);
            return objectMapper.readValue(json, javaType);
        } catch (JsonProcessingException e) {
            throw new JSONException("json to list error");
        }
    }

    /**
     * JSON字符串转Map
     *
     * @param json JSON字符串
     * @return Map对象
     */
    public static Map<String, Object> toMap(String json) {
        return toBean(json, new TypeReference<Map<String, Object>>() {
        });
    }

    /**
     * 将对象转换为 Map
     *
     * @param object 要转换的 Java 对象
     * @return 转换后的 Map 结构
     */
    public static Map<String, String> toMapStr(Object object) {
        return objectMapper.convertValue(object, new TypeReference<Map<String, String>>() {
        });
    }

    public static Map<String, Object> toMapObj(Object object) {
        return objectMapper.convertValue(object, new TypeReference<Map<String, Object>>() {
        });
    }

    /**
     * 对象深拷贝
     *
     * @param obj   原始对象
     * @param clazz 目标类型
     * @param <T>   泛型类型
     * @return 拷贝后的新对象
     */
    public static <T> T deepClone(Object obj, Class<T> clazz) {
        return toBean(toJsonStr(obj), clazz);
    }
}
